package github.flandre.examplemod.common.world;

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.INBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.DimensionSavedDataManager;
import net.minecraft.world.storage.WorldSavedData;
import org.jetbrains.annotations.NotNull;

public class ExampleWorldSaveData extends WorldSavedData {
    private static final String NAME = "DeepPowerWorldSaveData";
    private int power = 0;

    public ExampleWorldSaveData() {
        super(NAME);
    }

    public ExampleWorldSaveData(String name) {
        super(name);
    }

    public static ExampleWorldSaveData get(World worldIn) {
        if (!(worldIn instanceof ServerWorld)) {
            throw new RuntimeException("Attempted to get the data from a client world. This is wrong.");
        }
        ServerWorld world = (ServerWorld)worldIn;
        DimensionSavedDataManager storage = world.getSavedData();
        return storage.getOrCreate(ExampleWorldSaveData::new, NAME);
    }

    public int getPower() {
        return power;
    }

    public void setPower(int power) {
        this.power = power;
        markDirty();
    }

    @Override
    public void read(CompoundNBT nbt) {
        power = nbt.getInt("deeppower");
        markDirty();

    }

    @Override
    public @NotNull CompoundNBT write(CompoundNBT compound) {
        compound.putInt("deeppower",this.power);
        markDirty();
        return compound;
    }
}
